package com.info33.platform.business.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.info33.platform.business.app.dao.AppMenuDao;
import com.info33.platform.business.app.dao.AppMenuRoleDao;
import com.info33.platform.business.app.dao.AppTenantMenuDao;
import com.info33.platform.business.app.dto.AppTenantMenuDTO;
import com.info33.platform.business.app.entity.AppMenu;
import com.info33.platform.business.app.entity.AppMenuRole;
import com.info33.platform.business.app.entity.AppTenantMenu;
import com.info33.platform.business.app.service.AppTenantMenuService;
import com.info33.platform.business.app.vo.AppHomeContentVO;
import com.info33.platform.business.app.vo.AppHomeVO;
import com.info33.platform.business.app.vo.AppTenantMenuVO;
import com.info33.platform.common.constant.TableConstant;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.system.dao.SysAppMenuDao;
import com.info33.platform.system.vo.SysUserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author admin
 */
@Service
public class AppTenantMenuServiceImpl extends ServiceImpl<AppTenantMenuDao, AppTenantMenu> implements AppTenantMenuService {

    @Autowired
    private AppTenantMenuDao appTenantMenuDao;
    @Autowired
    private AppMenuDao appMenuDao;
    @Autowired
    private AppMenuRoleDao appMenuRoleDao;
    @Autowired
    SysAppMenuDao sysAppMenuDao;

    @Override
    public AppHomeVO appHome(SysUserVO currentUser) {
        AppHomeVO vo = new AppHomeVO();

        // 对首页内容进行排序
        return sortContent(vo);
    }

    /**
     * 对首页返回的数据进行排序
     *
     * @param vo 无序内容
     * @return 有序内容
     */
    private AppHomeVO sortContent(AppHomeVO vo) {
        List<Long> sort = Arrays.asList(15L, 26L, 5L, 18L, 20L, 10L, 25L);
        vo.getContent().forEach(l -> l.setSort(sort.indexOf(l.getLabel())));
        vo.getContent().sort(Comparator.comparing(AppHomeContentVO::getSort));
        return vo;
    }

    @Override
    public AppTenantMenuVO appMore(SysUserVO currentUser) {
        AppTenantMenuVO vo = new AppTenantMenuVO();
        return vo;
    }

    @Override
    public List<AppMenu> appHomeMenuToSort(SysUserVO currentUser) {
        return appMenuRoleDao.appHomeMenuToSort(currentUser);
    }

    @Override
    public IPage<AppTenantMenuVO> pcPage(Page<AppTenantMenu> page, AppTenantMenuDTO dto, SysUserVO currentUser) {
        return appTenantMenuDao.pcPage(page, dto, currentUser);
    }

    @Override
    public AppTenantMenuVO pcDetail(Long id) {
        AppTenantMenuVO vo = appTenantMenuDao.infoWithBase(id);
        vo.setRoleVOList(appTenantMenuDao.roleListById(id));
        vo.setAll(!ObjectUtils.isEmpty(appMenuRoleDao.selectOne(new QueryWrapper<AppMenuRole>()
                .select(TableConstant.ID)
                .eq("menu_id", id)
                .eq("type", 3)
                .last(TableConstant.LIMIT_ONE))));
        vo.setTenantUserVOList(appTenantMenuDao.userListById(id));
        return vo;
    }

    @Override
    public Result<Object> pcReset(Long id) {
        return this.update(new UpdateWrapper<AppTenantMenu>()
                .setSql("name=null, img_url=null, sort=1")
                .eq(TableConstant.ID, id)) ? Result.ok() : Result.error();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result<Object> pcEdit(AppTenantMenuDTO dto) {
        AppTenantMenu entity = new AppTenantMenu();
        BeanUtils.copyProperties(dto, entity);
        AppMenu appMenu = new AppMenu();
        appMenu.setId(dto.getMenuDTO().getId());
        appMenu.setUrl(dto.getMenuDTO().getUrl());
        appMenuDao.updateById(appMenu);
        return appTenantMenuDao.updateById(entity) != 1
                ? Result.error(Result.RESULT_ERROR_PUBLIC_MSG)
                : Result.ok();
    }

    @Override
    public List<AppTenantMenuVO> pcTree(Long tenantId) {
        List<AppTenantMenuVO> list = appTenantMenuDao.pcTree(tenantId);
        List<AppTenantMenuVO> collect = list.stream().filter(l -> l.getMenuVO().getPid().equals(1L)).collect(Collectors.toList());
        collect.forEach(l -> l.setTenantMenuVOS(list.stream().filter(ll -> ll.getMenuVO().getPid().equals(l.getMenuVO().getId())).collect(Collectors.toList())));
        return collect;
    }

    @Override
    public List<AppMenu> appHomeMenu(SysUserVO currentUser) {
        return appMenuRoleDao.appHomeMenu(currentUser);
    }

}